package org.dromara.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.iot.domain.IotFaultRecord;
import org.dromara.iot.domain.bo.IotFaultRecordBo;
import org.dromara.iot.domain.vo.IotFaultRecordVo;
import org.dromara.iot.mapper.IotFaultRecordMapper;
import org.dromara.iot.service.IIotFaultRecordService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 电梯维修记录Service业务层处理
 *
 * @author lsl
 * @date 2024-06-15
 */
@RequiredArgsConstructor
@Service
public class IotFaultRecordServiceImpl implements IIotFaultRecordService {

    private final IotFaultRecordMapper baseMapper;

    /**
     * 查询电梯维修记录
     */
    @Override
    public IotFaultRecordVo queryById(Long recordId) {
        return baseMapper.selectVoById(recordId);
    }

    /**
     * 查询电梯维修记录列表
     */
    @Override
    public TableDataInfo<IotFaultRecordVo> queryPageList(IotFaultRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IotFaultRecord> lqw = buildQueryWrapper(bo);
        Page<IotFaultRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询电梯维修记录列表
     */
    @Override
    public List<IotFaultRecordVo> queryList(IotFaultRecordBo bo) {
        LambdaQueryWrapper<IotFaultRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IotFaultRecord> buildQueryWrapper(IotFaultRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IotFaultRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getFaultId() != null, IotFaultRecord::getFaultId, bo.getFaultId());
        lqw.eq(StringUtils.isNotBlank(bo.getTiringState()), IotFaultRecord::getTiringState, bo.getTiringState());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), IotFaultRecord::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), IotFaultRecord::getLatitude, bo.getLatitude());
        return lqw;
    }

    /**
     * 新增电梯维修记录
     */
    @Override
    public Boolean insertByBo(IotFaultRecordBo bo) {
        IotFaultRecord add = MapstructUtils.convert(bo, IotFaultRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRecordId(add.getRecordId());
        }
        return flag;
    }

    /**
     * 修改电梯维修记录
     */
    @Override
    public Boolean updateByBo(IotFaultRecordBo bo) {
        IotFaultRecord update = MapstructUtils.convert(bo, IotFaultRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IotFaultRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除电梯维修记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<IotFaultRecordVo> queryListByRepairId(Long repairId) {
        LambdaQueryWrapper<IotFaultRecord> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(IotFaultRecord::getFaultId, repairId);
        return baseMapper.selectVoList(wrapper);
    }
}
